//1. 导入 axios
import axios from 'axios';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';

//2. 创建 axios 的实例
let instance = axios.create({
  baseURL: 'http://localhost:9000'
});

// 设置请求拦截器
instance.interceptors.request.use(config => {
  //携带公共参数
  config.headers.token = 'xxx';
  //开启 loading 效果
  NProgress.start();
  return config;
  // throw '出问题啦~~';
}, error => {
  throw error;
})

//设置响应拦截器
instance.interceptors.response.use(response => {
  //关闭 loading
  NProgress.done();
  //返回响应体的结果
  return response.data;
}, error => {
  //公共的错误处理
  alert("请求失败~~~");
  throw error;
});

//3. 暴露
export default instance;